package com.endbd.marketing.service.impl;

import com.endbd.feignapi.model.marketing.Custom;
import com.endbd.feignapi.model.marketing.Order;
import com.endbd.feignapi.model.marketing.Orderdetails;
import com.endbd.feignapi.model.purchase.CBrand;
import com.endbd.feignapi.model.purchase.CProduct;
import com.endbd.feignapi.model.purchase.CType;
import com.endbd.feignapi.model.system.City;
import com.endbd.feignapi.model.system.Job;
import com.endbd.feignapi.model.system.Province;
import com.endbd.feignapi.model.system.User;
import com.endbd.marketing.dao.IMarketingDao;
import com.endbd.marketing.model.MarketingCondition;
import com.endbd.marketing.model.MarketingOrdersCondition;
import com.endbd.marketing.model.MarketingOrdersDetailsCondition;
import com.endbd.marketing.model.PageBean;
import com.endbd.marketing.service.IMarketingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
@Service
public class MarketingService implements IMarketingService {
    @Autowired
    private IMarketingDao marketingDao;

    public IMarketingDao getMarketingDao() {
        return marketingDao;
    }

    public void setMarketingDao(IMarketingDao marketingDao) {
        this.marketingDao = marketingDao;
    }

    @Override
    public PageBean<Custom> queryMarketingPageByCondition(PageBean<Custom> pageBean, MarketingCondition conn) {
        int currentPage = pageBean.getCurrentPage();
        currentPage=currentPage<=0?1:currentPage;
        int rows = pageBean.getRows();
        rows=rows<=0?5:rows;
        PageHelper.startPage(currentPage,rows);
        List<Custom> users = marketingDao.queryMarketingListByCondition(conn);
        PageInfo<Custom> pb=new PageInfo<>(users);
        pageBean.setCurrentPage(pb.getPageNum());
        pageBean.setRows(pb.getPageSize());
        pageBean.setTotalPage(pb.getPages());
        pageBean.setTotalCount((int)pb.getTotal());
        pageBean.setList(new ArrayList<Custom>(pb.getList()));

        return pageBean;
    }

    @Override
    public List<Custom> queryMarketingListByCondition(MarketingCondition conn) {
        List<Custom> customs = marketingDao.queryMarketingListByCondition(conn);
        return customs;
    }

    @Override
    public Custom queryMarketingViewByCondition(MarketingCondition conn) {
        Custom custom = marketingDao.queryMarketingViewByCondition(conn);
        return custom;
    }

    @Override
    public List<City> queryCity(MarketingCondition conn) {
        List<City> cities = marketingDao.queryCity(conn);
        return cities;
    }

    @Override
    public City queryCityByMarketingCondition(MarketingCondition conn) {
        City city = marketingDao.queryCityByMarketingCondition(conn);
        return city;
    }

    @Override
    public User queryUserBycreateId(Integer createId){
        User user = marketingDao.queryUserBycreateId(createId);
        return user;
    }

    @Override
    public void updateStatus(MarketingCondition conn) {
        marketingDao.updateStatus(conn);
    }

    @Override
    public List<User> queryName(MarketingCondition conn) {
        List<User> users = marketingDao.queryName(conn);
        return users;
    }

    @Override
    public void assignment(MarketingCondition conn) {
        marketingDao.assignment(conn);
    }

    @Override
    public void updateMarketingCustom(Custom custom) {
        marketingDao.updateMarketingCustom(custom);
    }

    @Override
    public void addMarketingCustom(Custom custom) {
        marketingDao.addMarketingCustom(custom);
    }

    @Override
    public PageBean<Order> queryMarketingOrdersListByCondition(PageBean<Order> pageBean, MarketingOrdersCondition conn) {
        int currentPage = pageBean.getCurrentPage();
        currentPage=currentPage<=0?1:currentPage;
        int rows = pageBean.getRows();
        rows=rows<=0?5:rows;
        PageHelper.startPage(currentPage,rows);
        List<Order> orders = marketingDao.queryMarketingOrdersListByCondition(conn);
        PageInfo<Order> pb=new PageInfo<>(orders);
        pageBean.setCurrentPage(pb.getPageNum());
        pageBean.setRows(pb.getPageSize());
        pageBean.setTotalPage(pb.getPages());
        pageBean.setTotalCount((int)pb.getTotal());
        pageBean.setList(new ArrayList<Order>(pb.getList()));

        return pageBean;
    }

    @Override
    public List<Order> queryMarketingOrdersListByCondition(MarketingOrdersCondition conn) {
        List<Order> orders = marketingDao.queryMarketingOrdersListByCondition(conn);
        return orders ;
    }

    @Override
    public List<CBrand> queryBrand() {
        List<CBrand> cBrands = marketingDao.queryBrand();
        return cBrands;
    }

    @Override
    public List<CType> queryTypeByBrandId(Integer brandId) {
        List<CType> cTypes = marketingDao.queryTypeByBrandId(brandId);
        return cTypes;
    }

    @Override
    public List<CProduct> queryProductByTypeId(Integer typeId) {
        List<CProduct> cProducts = marketingDao.queryProductByTypeId(typeId);
        return cProducts;
    }

    @Override
    public List<CProduct> queryProductByproductId(Integer productId) {
        List<CProduct> cProducts = marketingDao.queryProductByproductId(productId);
        return cProducts;
    }

    @Override
    public List<Job> queryJobByDeptId(Integer deptId) {
        List<Job> jobs = marketingDao.queryJobByDeptId(deptId);
        return jobs;
    }

    @Override
    public List<User> queryUserByJobId(Integer jobId) {
        List<User> users = marketingDao.queryUserByJobId(jobId);
        return users;
    }

    @Override
    public void addOrder(MarketingOrdersCondition conn) {
        marketingDao.addOrder(conn);
    }

    @Override
    public void addOrderDetail(Orderdetails orderdetails) {
        marketingDao.addOrderDetail(orderdetails);
    }

    @Override
    public List<Orderdetails> queryOrderDetailsByOrderId(MarketingOrdersDetailsCondition conn) {
        List<Orderdetails> orderdetails = marketingDao.queryOrderDetailsByOrderId(conn);
        return orderdetails;
    }

    @Override
    public void delOrder(String orderId) {
        marketingDao.delOrder(orderId);
    }

    @Override
    public void delOrderDetail(String orderId) {
        marketingDao.delOrderDetail(orderId);
    }

    @Override
    public void orderExamine(Order order) {
        marketingDao.orderExamine(order);
    }

    @Override
    public void orderAudit(Order order) {
        marketingDao.orderAudit(order);
    }

    @Override
    public Custom queryAddressByCustomId(Custom custom) {
        Custom custom1 = marketingDao.queryAddressByCustomId(custom);
        return custom1;
    }

    @Override
    public void updateOrder(MarketingOrdersCondition conn) {
        marketingDao.updateOrder(conn);
    }

    @Override
    public void updateOrderWarehouse(MarketingOrdersCondition conn) {
        marketingDao.updateOrderWarehouse(conn);
    }

    @Override
    public void addOrderWarehouse(MarketingOrdersCondition conn) {
        marketingDao.addOrderWarehouse(conn);
    }
}
